package com.towexin.application.system.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.towexin.application.system.domain.Menu;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface MenuMapper extends BaseMapper<Menu> {

	// 根据用户名获取自己有权查到的菜单列表
	@Select("SELECT a.* FROM t_menu a LEFT JOIN t_authority_menu b ON a.id = b.m_id LEFT JOIN t_role_authority c ON b.a_id = c.a_id LEFT JOIN t_role d ON d.id = c.r_id LEFT JOIN t_user_role e ON e.r_id = d.id LEFT JOIN t_user f ON f.id = e.u_id WHERE f.username = #{userName} AND d.status=TRUE UNION ALL SELECT a.* FROM t_menu a WHERE a.id NOT IN (SELECT m_id FROM t_authority_menu) ORDER BY sort ASC")
	List<Menu> selectAuthorityMenuByUserName(String userName);

	@Select("SELECT a.* FROM t_menu a LEFT JOIN t_authority_menu b ON a.id = b.m_id LEFT JOIN t_role_authority c ON b.a_id = c.a_id LEFT JOIN t_role d ON d.id = c.r_id WHERE d.role_type='system_default_authorty' AND d.status=TRUE UNION ALL SELECT a.* FROM t_menu a WHERE a.id NOT IN (SELECT m_id FROM t_authority_menu) ORDER BY sort ASC")
	List<Menu> selectDefaultMenu();

	@Select("SELECT * FROM t_menu WHERE type=1 AND status=1")
	Menu selectSystemHome();
}
